package com.muy.redis.view.window.redis.view.table.bean;

import com.muy.redis.view.window.redis.enums.RedisValueTypeEnum;
import com.muy.redis.view.window.redis.view.table.AColumnDesc;
import com.muy.redis.view.window.redis.view.table.ColumnShowAttr;
import com.muy.redis.view.window.redis.view.table.TreeLineObject;
import org.apache.commons.compress.utils.Lists;

import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * @Author jiyanghuang
 * @Date 2023/9/17 13:31
 */
public class RedisHashRow extends AbstractTableStringRow {

    @AColumnDesc(nameShow = "Row", order = 1, cellEditable = false, columnShowAttr = ColumnShowAttr.INDEX)
    private Integer row;

    @AColumnDesc(nameShow = "Field", order = 2, vcFun = "updateValue")
    private String field;

    @AColumnDesc(nameShow = "Value", order = 10, vcFun = "updateValue")
    private String value;

    private RedisValueTypeEnum typeEnum;

    private BiConsumer<RedisHashRow, RedisHashRow> updateConsumer;

    public static List<RedisHashRow> genTableData(List<Map.Entry<String, String>> strings, RedisValueTypeEnum typeEnum, BiConsumer<RedisHashRow, RedisHashRow> updateConsumer) {
        List<RedisHashRow> rows = Lists.newArrayList();
        for (int i = 0; i < strings.size(); i++) {
            RedisHashRow redisListRow = new RedisHashRow();
            Map.Entry<String, String> entry = strings.get(i);
            redisListRow.setRow(i);
            redisListRow.setValue(entry.getValue());
            redisListRow.setField(entry.getKey());
            redisListRow.setTypeEnum(typeEnum);
            redisListRow.setUpdateConsumer(updateConsumer);
            rows.add(redisListRow);
        }
        return rows;
    }

    public void updateValue(RedisHashRow old, RedisHashRow update) {
        updateConsumer.accept(old, update);
    }

    @Override
    public <T extends TreeLineObject> T copyThis() {
        RedisHashRow redisListRow = new RedisHashRow();
        redisListRow.setRow(this.getRow());
        redisListRow.setValue(this.getValue());
        redisListRow.setTypeEnum(this.getTypeEnum());
        redisListRow.setField(this.getField());
        return (T) redisListRow;
    }


    public Integer getRow() {
        return row;
    }

    public void setRow(Integer row) {
        this.row = row;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public RedisValueTypeEnum getTypeEnum() {
        return typeEnum;
    }

    public void setTypeEnum(RedisValueTypeEnum typeEnum) {
        this.typeEnum = typeEnum;
    }

    public BiConsumer<RedisHashRow, RedisHashRow> getUpdateConsumer() {
        return updateConsumer;
    }

    public void setUpdateConsumer(BiConsumer<RedisHashRow, RedisHashRow> updateConsumer) {
        this.updateConsumer = updateConsumer;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }
}
